<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
    <style>
        #div{position: absolute; width: 100px;height: 100px;background: #09f;}
        #div:active{background:red;}
    </style>
</head>
<body>
    <div id="div"></div>

    <script>
        let oDiv = document.getElementById('div');

        oDiv.onmousedown = function(event){
            var disX = event.pageX - oDiv.offsetLeft;
            var disY = event.pageY - oDiv.offsetTop;
            document.onmousemove = function(event){
                oDiv.style.left = event.pageX - disX + 'px';
                oDiv.style.top = event.pageY - disY + 'px';
            }
            document.onmouseup = function(){
                document.onmousemove = document.onmouseup = null;
            }
        }


    </script>
</body>
</html>